/*
 * ﻿Copyright (C) 2011-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.aspectjutil.eventmanager;

import java.lang.reflect.Method;

import junit.framework.Assert;

import org.junit.Test;

public class EventHandlerTestWithEventProducerWrapperAspect extends EventHandlerTestTemplate {
	// Test whether aspect instance can be event listener -----------------------------------------
	@Test
	public void testAspectTypeEventListener() throws Throwable {
		// Execute EventProducerWrapper.calculate method ------------------------------------------
		Object eventProducerWrapperObj 
		= EventManagerTestUtils.eventProducerWrapperFactory( childDelegationClassLoader);
		Method calculateMethod 
		= EventManagerTestUtils.getEventProducerWrapperCalculateMethod( eventProducerWrapperObj.getClass());
		Integer calculationResult = (Integer)calculateMethod.invoke( eventProducerWrapperObj, 4, 8);
		// ----------------------------------------------------------------------------------------
		
		Class<?> eventProducerWrapperAspectClass 
		= EventManagerTestUtils.getEventProducerWrapperAspectClass( childDelegationClassLoader);
		Method getEventInfoMethod
		= EventManagerTestUtils.getEventProducerWrapperAspectGetEventInfoMethod( 
				eventProducerWrapperAspectClass);
		Object eventInfoObj = getEventInfoMethod.invoke( null, new Object[]{});
		Assert.assertNotNull( eventInfoObj);
		
		Class<?> calculationEventClass 
		= EventManagerTestUtils.getCalculationEventClass( childDelegationClassLoader);
		Method getTriggeredEventMethod 
		= EventManagerTestUtils.getEventInfoGetTriggeredEventMethod( eventInfoObj.getClass());
		Assert.assertEquals( 
				calculationEventClass,
				getTriggeredEventMethod.invoke( eventInfoObj, new Object[]{})
				);
		
		Method getEventerInfoMethod 
		= EventManagerTestUtils.getEventInfoGetEventerInfoMethod( eventInfoObj.getClass());
		Object eventerInfoObj = getEventerInfoMethod.invoke( eventInfoObj, new Object[]{});
		Assert.assertEquals( 
				EventManagerTestUtils.getEventerType( "Method", childDelegationClassLoader), 
				EventManagerTestUtils.getEventerTypeOfEventerInfo( eventerInfoObj)
				);
		
		Assert.assertEquals( 
				EventManagerTestUtils.getEventProducerClass( childDelegationClassLoader),
				EventManagerTestUtils.getEventerObjOfEventerInfo( eventerInfoObj).getClass()
				);
		
		Assert.assertEquals( 
				"calculate", 
				EventManagerTestUtils.getEventerMethodOfEventerInfo( eventerInfoObj).getName()
				);
	}
	// --------------------------------------------------------------------------------------------
}
